﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class RewardAdd : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String saveDir = @"\uploads\reward\";
            string appPath = Request.PhysicalApplicationPath;

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + saveDir + Server.HtmlEncode(FileUpload1.FileName);
                String fileName = FileUpload1.FileName;

                FileUpload1.SaveAs(savePath);

                UploadStatusLabel.Text = "Your file was saved as ";
                imgName.Text = fileName;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
                SqlCommand cmdInsert;
                conn.Open();

                cmdInsert = new SqlCommand("INSERT INTO Reward VALUES (@name, @desc, @points, @quan, @pic)", conn);
                cmdInsert.Parameters.AddWithValue("@name", txtPname.Text);
                cmdInsert.Parameters.AddWithValue("@desc", txtPdesc.Text);
                cmdInsert.Parameters.AddWithValue("@points", txtPoints.Text);
                cmdInsert.Parameters.AddWithValue("@quan", txtStock.Text);
                cmdInsert.Parameters.AddWithValue("@pic", imgName.Text);
                int success = cmdInsert.ExecuteNonQuery();

                if (success == 1)
                {
                    MessageBox("Upload Successfully!");
                    Redirect("RewardsList.aspx");
                }

                conn.Close();
          
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}